
import { http } from '../http/api'
var config = {
  key: 'xxxxxxxxxxxxx'
}

export const getLocationUrl =  (latitude, longitude) => (`https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}`)

/**
 * 检测是否为空
 */
export const isNull = (e) => {
  return e === undefined || e === 'undefined' || e == null || e === '' || e === 0
}

/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
  if (!name) return
  if (typeof content !== 'string') {
    content = JSON.stringify(content)
  }
  window.localStorage.setItem(name, content)
}

/**
 * 获取localStorage
 */
export const getStore = name => {
  if (!name) return
  return window.localStorage.getItem(name)
}

/**
 * 删除localStorage
 */
export const removeStore = name => {
  if (!name) return
  window.localStorage.removeItem(name)
}

/**
 * 获取？号后面的参数
 */
export const getQueryString = (name) => {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  var r = window.location.search.substr(1).match(reg)
  if (r != null) return unescape(r[2])
  return null
}


/**
 * 经纬度
 */
export const jssdkGetLocation = () => {
  return new Promise((resolve, reject) => {
    var url = location.href.split('#')[0]
    http.get('/wx/getJsapiSignature',{url}).then((res) => {
      window.wx.config({
        debug: false,
        appId: res.data.appId,
        timestamp: res.data.timestamp,
        nonceStr: res.data.nonceStr,
        signature: res.data.signature,
        jsApiList: ['getLocation']
      })
      window.wx.ready(() => {
        window.wx.getLocation({
          type: 'wgs84',
          success: (res) => {
            console.log(res, '$$成功定位$$')
            resolve(res)
          },
          complete: function (res) {
            // 接口调用完成时执行的回调函数，无论成功或失败都会执行。
          },
          fail: (res) => {
            console.log(res, '$$拒绝定位$$')
            resolve(res)
          },
          cancel: (res) => {
            resolve(res)
          }
        })
      })
    })
  })
}


/**
 * 支付
 */
export const pay = ({orderId}) => {
  return new Promise((resolve, reject) => {
    http.put(`/wx/pay/unifiedOrder?orderId=${orderId}`).then((res) => {
      var params = res.data
      console.log(params, '我是支付取的参数')
      window.wx.ready(() => {
        window.wx.chooseWXPay({
          appId: params.appId,
          timestamp: params.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
          nonceStr: params.nonceStr, // 支付签名随机串，不长于 32 位
          package: params.packageValue, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
          signType: params.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
          paySign: params.paySign, // 支付签名
          success: (res) => {
            resolve()
          },
          complete: function (res) {
            // 接口调用完成时执行的回调函数，无论成功或失败都会执行。
          },
          fail: (res) => {
            console.log(res, '&&支付失败&&')
            reject(res)
          },
          cancel: (res) => {
            console.log(res, '&&取消支付&&')
            reject(res)
          }
        })
      })
      window.wx.error((res) => {
        console.log(res.err_msg)
      })
    })
  })
}


